package dataStructure.tree;


/**
 * @author 浦希成
 * 2019-01-24
 */
public class 求最近的节点数量 {
    public static int run(TreeNode root) {
        if (root == null) {
            return 0;
        }
        if (root.left == null && root.right == null) {
            return 1;
        }
        return depth(root);
    }

    public static int depth(TreeNode root) {
        if (root.left == null && root.right == null) {
            return 1;
        }
        if (root.left == null) {
            return depth(root.right) + 1;
        }
        if (root.right == null) {
            return depth(root.left) + 1;
        }

        return Math.min(depth(root.left), depth(root.right)) + 1;
    }

    public static void main(String[] args) {
        //创建二叉树
        TreeNode root = Util.createTree();
        System.out.println(run(root));
    }
}

